var merchantId;
var merchantNte;
var userId;
var openId;
var id;
var app = getApp();
var url = app.globalData.url;
var that;
const defHeaders = {
    'Content-Type': 'application/json;charset=UTF-8',
    'Accept': 'application/json',
};
Page({
    data: {
        list: [],
        list_hui: [],
        display: "",
        display_one: "",
        display_two: "none",
        display_three: "",
        zIndex: "",
        absolute: "",
        extradata: '',
    },
    onLoad(options) {

        that = this;
        // 获取会员卡传入的参数
        wx.getStorage({
            key: 'merchantId',
            success: function (res) {
                merchantId = res.data.merchantId;
                wx.request({
                    url: url + '/card/getWXCard.do',
                    method: 'GET',
                    headers: {
                        defHeaders,
                    },
                    data: {
                        merchantId: merchantId,
                    },
                    dataType: 'json',
                    success: function (res) {
                        // console.log("1111111");
                        // console.log(res.data.body);
                        that.setData({
                            extradata: res.data.body
                        })
                        that.getAreMembers();
                    },
                    fail: function (res) {
                        console.log(res)
                        return '请求加入会员失败'
                    },
                    complete: function (res) {
                        // my.hideLoading();
                    }
                })
            }
        })



        //   //获取用户授权
        //获取用户授权
        wx.getStorage({
            key: 'openId',
            success(res) {
                if (res.data.openId != "") {
                    that.getAreMembers();
                } else {
                    var code
                    // 发起授权
                    wx.login({
                        success(res) {
                            console.log("code")
                            code = res.code
                            console.log(res)
                            if (res.code) {
                                // 发起网络请求
                                wx.request({
                                    url: url + '/auth/getWxOpenId.do',
                                    method: 'GET',
                                    data: {
                                        auth_code: code,
                                        app_id: "wxccca394cfab6aa7d"
                                    },
                                    headers: {
                                        defHeaders,
                                    },
                                    dataType: 'json',
                                    success: function (res) {
                                        console.log(6666)
                                        // console.log("获取token: " + JSON.stringify(res))
                                        wx.setStorage({
                                            key: 'openId',
                                            data: {
                                                openId: res.data.body.openid,
                                                // session_key: res.data.body.session_key,
                                            },
                                            success: function (res) {
                                                that.getAreMembers();
                                            }
                                        })
                                    },
                                    fail: function (res) {
                                        // wx.showModal({
                                        //   content: JSON.stringify(res),
                                        // });
                                        console.log(res)
                                    },
                                    complete: function (res) {
                                    }
                                })
                            } else {
                                console.log('登录失败！' + res.errMsg)
                            }
                        }
                    })
                }

            },
            fail(res) {
                var code
                // 发起授权
                wx.login({
                    success(res) {
                        console.log("code")
                        code = res.code
                        console.log(res)
                        if (res.code) {
                            // 发起网络请求
                            wx.request({
                                url: url + '/auth/getWxOpenId.do',
                                method: 'GET',
                                data: {
                                    auth_code: code,
                                    app_id: "wxccca394cfab6aa7d"
                                },
                                headers: {
                                    defHeaders,
                                },
                                dataType: 'json',
                                success: function (res) {
                                    console.log(6666)
                                    openId = res.data.body.openid
                                    console.log("获取token: " + JSON.stringify(res))
                                    wx.setStorage({
                                        key: 'openId',
                                        data: {
                                            openId: res.data.body.openid,
                                            // session_key: res.data.body.session_key,
                                        },
                                        success: function (res) {
                                            // wx.getUserInfo({
                                            //   success(res) {
                                            //     console.log(999999)
                                            //     console.log(res)
                                            //  拼接钱
                                            that.getAreMembers();
                                            //   }
                                            // })
                                        }
                                    })
                                },
                                fail: function (res) {
                                    // wx.showModal({
                                    //   content: JSON.stringify(res),
                                    // });
                                    console.log(res)
                                },
                                complete: function (res) {
                                }
                            })
                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
            },
        })

    },
    tiao(){
        wx.setStorage({
            key: 'type',
            data: {
                type:3,
            },
        })
    },
    //获取用户身份
    getAreMembers() {
        // 取出userId
        wx.getStorage({
            key: 'userId',
            success: function (res) {
                // 切换页面
                userId = res.data.userId
                console.log("userId")
                console.log(userId)
                // userId = res.data.userId;
                // my.alert({ title:"userId"+userId})
                // 取出merchantId
                wx.getStorage({
                    key: 'merchantId',
                    success: function (res) {
                        merchantId = res.data.merchantId;
                        //  my.alert({ title:"merchantId"+merchantId})
                        // my.alert({ title: merchantId })
                        wx.request({
                            url: url + '/card/getCardTemplate.do', // 目标服务器url
                            method: 'get',
                            data: {
                                merchantId: merchantId,
                            },
                            headers: {
                                defHeaders
                            },
                            dataType: 'json',
                            success: (res) => {
                                //  my.alert({ title:"res"+res})
                                console.log(merchantId)
                                if (res.data.code == 1) {
                                    //获取会员卡信息
                                    wx.request({
                                        url: url + '/card/getCard.do',
                                        method: 'get',
                                        data: {
                                            merchantId: merchantId,
                                            userId: userId
                                        },
                                        headers: {
                                            defHeaders
                                        },
                                        dataType: 'json',
                                        success: function (res) {
                                            if (typeof (res.data.body) == "undefined") {
                                                //未领卡
                                                wx.request({

                                                    url: url + '/cardMembership/getCardDepositConfig.do',
                                                    method: 'get',
                                                    data: {
                                                        merchantId: merchantId,
                                                    },
                                                    headers: {
                                                        defHeaders
                                                    },
                                                    dataType: 'json',
                                                    success: function (res) {
                                                        // console.log(res)
                                                        var arr = [];
                                                        var list = res.data.body.cardDepositBenefitConfigList
                                                        for (var i = 0; i < list.length; i++) {
                                                            list[i].name = list[i].comment;
                                                            arr.push(list[i]);
                                                        }
                                                        // console.log(name)
                                                        that.setData({
                                                            list: arr,
                                                        });
                                                    },
                                                    fail: function (res) {
                                                        my.alert({
                                                            content: res,
                                                        });
                                                    },
                                                    complete: function (res) {
                                                        // my.hideLoading();
                                                    }
                                                });
                                                // 等级权益
                                                wx.request({

                                                    url: url + '/cardMembership/getList.do',
                                                    method: 'get',
                                                    data: {
                                                        merchantId: merchantId,
                                                    },
                                                    headers: {
                                                        'Content-Type': 'application/json;charset=UTF-8',
                                                        'Accept': 'application/json',
                                                    },
                                                    dataType: 'json',
                                                    success: function (res) {
                                                        console.log(res)
                                                        var arr = [];
                                                        var list = res.data.body
                                                        for (var j = 0; j < list.length; j++) {
                                                            list[j].title = list[j].name;
                                                            list[j].growthBalance = list[j].growthBalance;
                                                            list[j].growthPoint = list[j].growthPoint;
                                                            list[j].ruleBalance = list[j].ruleBalance;
                                                            list[j].rulePoint = list[j].rulePoint;
                                                            list[j].exchangePoint = list[j].exchangePoint;
                                                            list[j].exchangeBalance = list[j].exchangeBalance;
                                                            arr.push(list[j]);

                                                        }
                                                        // console.log(name)
                                                        that.setData({
                                                            list_hui: arr,
                                                        });
                                                    },
                                                    fail: function (res) {
                                                        // my.alert({
                                                        //   content: res,
                                                        // });
                                                    },
                                                    complete: function (res) {
                                                        // my.hideLoading();
                                                    }
                                                });
                                                that.setData({
                                                    // display: "none",
                                                    display_one: "none",
                                                    display_two: "none",
                                                    display_three: "block"
                                                })
                                            } else {
                                                var id = res.data.body.id;
                                                wx.setStorage({
                                                    key: 'id',
                                                    data: {
                                                        id: id,
                                                    },
                                                    success: function () {
                                                    }
                                                });
                                                if (!res.data.body.unUseAmount || typeof (res.data.body.unUseAmount) == "underfind") {
                                                    var all_pay = res.data.body.balance;
                                                    setTimeout(() => {
                                                        that.setData({
                                                            no_pay: "0",
                                                            all_pay: all_pay,
                                                            data: res.data.body,
                                                            display: "none",
                                                            display_one: "block",
                                                            display_two: "none",
                                                            display_three: "none"
                                                        })
                                                        // my.hideLoading({});
                                                    }, 500);
                                                } else {
                                                    var no_pay = res.data.body.unUseAmount;
                                                    var all_pay = parseFloat(no_pay) + parseFloat(res.data.body.balance);
                                                    setTimeout(() => {
                                                        that.setData({
                                                            no_pay: no_pay,
                                                            all_pay: all_pay,
                                                            data: res.data.body,
                                                            display: "none",
                                                            display_one: "block",
                                                            display_two: "none",
                                                            display_three: "none"
                                                        })
                                                        // my.hideLoading({});
                                                    }, 500);
                                                }

                                            }
                                            // setTimeout(() => {
                                            //   my.hideLoading({
                                            //   });
                                            that.setData({
                                                display: "none",
                                            })
                                            // }, 500);
                                        },
                                        fail: function (res) {
                                            // my.alert({
                                            //   content: res,
                                            // });
                                        },
                                        complete: function (res) {

                                        }
                                    });

                                } else {
                                    wx.getStorage({
                                        key: 'merchantName',
                                        success: function (res) {
                                            // my.hideLoading({
                                            // });
                                            that.setData({
                                                merchantName: res.data.merchantName,
                                                display: "none",
                                                display_one: "none",
                                                display_two: "block",
                                                display_three: "none"

                                            })
                                        },
                                        fail: function (res) {
                                            // my.alert({ content: "加载失败" });
                                        }
                                    });


                                }

                            },
                            fail: function (res) {
                                // my.alert({ content: "加载失败" });
                            }
                        });
                        // console.log(merchantId)



                        // 页面加载
                        // my.showLoading({
                        //   content: '加载中...',
                        //   delay: 0,
                        //   success: () => {

                        //   }
                        // })
                        //判断是否有会员

                        console.log(res.data.code)
                        // res.data.code=1

                    },
                });
            },
            fail: function (res) {
                // my.alert({ content: "加载失败" });
            }
        });
        console.log(userId)

    },


    //领取会员卡
    receive: function () {

    },
    // 付款
    defaultTap() {

        wx.switchTab({
            url: '../pay/pay',   //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
        })

    },
    onShow(data) {
        // 页面显示
        that = this;
        that.getAreMembers();
        // wx.getStorage({
        //   key: 'userId',
        //   success: function (res) {
        //     if (!res.data.userId || typeof (res.data.userId) == "underfind") {
        //     } else {
        //       userId = res.data.userId;
        //       wx.request({
        //         url: url + '/card/getCard.do',
        //         method: 'get',
        //         data: {
        //           merchantId: merchantId,
        //           userId: userId
        //         },
        //         headers: {
        //           defHeaders
        //         },
        //         dataType: 'json',
        //         success: function (res) {
        //           console.log(999999)
        //           console.log(res);
        //           if (typeof (res.data.body) != "undefined") {
        //             var id = res.data.body.id;
        //             wx.setStorage({
        //               key: 'id',
        //               data: {
        //                 id: id,
        //               },
        //               success: function () {
        //               }
        //             });
        //             that.setData({
        //               data: res.data.body,
        //             })
        //           }

        //           wx.stopPullDownRefresh()
        //         }
        //       })
        //     }
        //   }
        // }) 
        // wx.getStorage({
        //     key: 'storeId',
        //     success: function (res) {
        //         var storeId = res.data.storeId
        //         if (storeId == 296731217806573568) {
        //             wx.redirectTo({
        //                 url: '../recommend/recommend',
        //             })
        //         }

        //     },
        //     fail: function (res) {
        //         // my.alert({ concat: "获得storeId失败" });
        //     }
        // });
    },
    onPullDownRefresh() {
        // 页面显示
        that = this;
        wx.getStorage({
            key: 'userId',
            success: function (res) {
                if (!res.data.userId || typeof (res.data.userId) == "underfind") {
                } else {
                    userId = res.data.userId;
                    wx.request({
                        url: url + '/card/getCard.do',
                        method: 'get',
                        data: {
                            merchantId: merchantId,
                            userId: userId
                        },
                        headers: {
                            defHeaders
                        },
                        dataType: 'json',
                        success: function (res) {
                            console.log(999999)
                            console.log(res);
                            if (typeof (res.data.body) != "undefined") {
                                var id = res.data.body.id;
                                wx.setStorage({
                                    key: 'id',
                                    data: {
                                        id: id,
                                    },
                                    success: function () {
                                    }
                                });
                                if (!res.data.body.unUseAmount || typeof (res.data.body.unUseAmount) == "underfind") {
                                    var all_pay = res.data.body.balance;
                                    console.log(all_pay)
                                    setTimeout(() => {
                                        that.setData({
                                            no_pay: "0",
                                            all_pay: all_pay,
                                            data: res.data.body,
                                            display: "none",
                                            display_one: "block",
                                            display_two: "none",
                                            display_three: "none"
                                        })
                                        // my.hideLoading({});
                                    }, 500);
                                } else {
                                    var no_pay = res.data.body.unUseAmount;
                                    var all_pay = parseFloat(no_pay) + parseFloat(res.data.body.balance);
                                    setTimeout(() => {
                                        that.setData({
                                            no_pay: no_pay,
                                            all_pay: all_pay,
                                            data: res.data.body,
                                            display: "none",
                                            display_one: "block",
                                            display_two: "none",
                                            display_three: "none"
                                        })
                                        // my.hideLoading({});
                                    }, 500);
                                }
                            }

                            wx.stopPullDownRefresh()
                        }
                    })
                }
            }
        })
    },
    // 点击跳转余额详情页面
    all_pay() {
        wx.navigateTo({
            url: '../balance/balance',
        })
    }
});
